package com.meitianhui.convenitenbuy.module.mine.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.mine.adapter.ShopGuiderAdapter;
import com.meitianhui.convenitenbuy.module.mine.contract.ShopGuiderContract;
import com.meitianhui.convenitenbuy.module.mine.di.DaggerMineComponent;
import com.meitianhui.convenitenbuy.module.mine.di.MineModule;
import com.meitianhui.convenitenbuy.module.mine.model.ShopGuiderBean;
import com.meitianhui.convenitenbuy.module.mine.presenter.ShopGuiderPresenter;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.widget.dialog.SettingStoreDialog;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/*
  Created by amy on 2017/11/27.
 */

public class ShopperGuideActivity extends BaseToolbarActivity implements ShopGuiderContract.View, BaseQuickAdapter.RequestLoadMoreListener {
    private LinearLayout mLlShopperNone;
    private RecyclerView mRlvShopperList;
    private Button mBtnShopperSearchStore;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<ShopGuiderBean.Convient> mShopGuiderList = new ArrayList<>();
    @Inject
    ShopGuiderPresenter mShopGuiderPresenter;
    private ShopGuiderAdapter mShopGuiderAdapter;
    private SettingStoreDialog mConfirmBindDialog;

    @Override
    protected int setView() {
        return R.layout.activity_shopper_guide;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerMineComponent.builder().appComponent(App.getInstance().getAppComponent()).mineModule(new MineModule(this)).build().inject(this);
        initId();
        setTbTitle("成为导购");
        setTbMenu(R.mipmap.icon_title_detail);
        initData();
        isEnable(false);
    }

    private void initData() {
        if (mShopGuiderAdapter == null) {
            mShopGuiderAdapter = new ShopGuiderAdapter(mShopGuiderList,"bind");
        }
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRlvShopperList.setLayoutManager(manager);
        mRlvShopperList.setAdapter(mShopGuiderAdapter);
        //adapter的加载更多
        mShopGuiderAdapter.setOnLoadMoreListener(this,mRlvShopperList);

        getShopGuider();

        //设置每个item里面的button的点击事件
        mShopGuiderAdapter.setOnClickButtonListener(new ShopGuiderAdapter.OnClickButtonListener() {
            @Override
            public void clickBindOrUnbind(int position) {


                mConfirmBindDialog = new SettingStoreDialog(ShopperGuideActivity.this, R.style.dialog_with_alpha, new SettingStoreDialog.AlertDialogListener() {
                    @Override
                    public void onConfirmClick() {

                        ShopGuiderBean.Convient convient = mShopGuiderList.get(position);
                        mShopGuiderPresenter.clickBindEvent(convient);
                    }

                    @Override
                    public void onCancelClick() {
                        mConfirmBindDialog.dismiss();
                    }
                });
                mConfirmBindDialog.show();
                mConfirmBindDialog.showCancelBtn(true);
                mConfirmBindDialog.setDialogTitle("提示");
                mConfirmBindDialog.setDialogContent("确定解除导购?");
            }
        });

    }

    /**
     * 获取数据
     */
    private void getShopGuider() {
        mShopGuiderPresenter.getShopGuiderList(pageIndex, pageSize);
    }

    private void initId() {
        mLlShopperNone = getViewById(R.id.ll_shopper_none);
        mRlvShopperList = getViewById(R.id.rlv_shopper_list);
        mBtnShopperSearchStore = getViewById(R.id.btn_shopper_search_store);

        //rxJava的仿手抖
        RxUtils.bindEvents(mBtnShopperSearchStore, this);
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()){
            case R.id.btn_shopper_search_store:
                //搜索门店
                Intent intent = new Intent(this, SearchGuiderStoreActivity.class);
                startActivity(intent);
                break;
        }
    }

    /**
     * 点击了菜单按钮
     */
    @Override
    protected void clickMenu() {
        super.clickMenu();
        Intent intent = new Intent(this, GuiderHistoryRequestActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onRefreshData() {

    }

    @Override
    public void showError(String msg) {
        TipUtils.showToast(App.getInstance(),msg,3);
    }

    @Override
    public void showEmpty() {
        mLlShopperNone.setVisibility(View.VISIBLE);
        mRlvShopperList.setVisibility(View.INVISIBLE);
        TipUtils.showToast(App.getInstance(),"暂无导购信息",3);
    }
    /**
     * 获取成为店铺导购信息成功
     * @param shopGuiderList
     */
    @Override
    public void getShopGuiderListSucc(List<ShopGuiderBean.Convient> shopGuiderList) {
        if (pageIndex == 1) {
            mShopGuiderList.clear();
        }
        mShopGuiderList.addAll(shopGuiderList);


        mLlShopperNone.setVisibility(View.INVISIBLE);
        mRlvShopperList.setVisibility(View.VISIBLE);
        //设置适配器

        if (mShopGuiderAdapter.isLoading())
            mShopGuiderAdapter.loadMoreComplete();
        if (pageIndex == 1)
            mShopGuiderAdapter.replaceData(shopGuiderList);
        else
            mShopGuiderAdapter.addData(shopGuiderList);
        //设置是否能加载更多
        mShopGuiderAdapter.setEnableLoadMore(shopGuiderList.size() < pageSize ? false:true);

    }

    /**
     * 删除导购成功
     * @param convient
     */
    @Override
    public void deleteGuiderSucc(ShopGuiderBean.Convient convient) {
        mShopGuiderList.remove(convient);
        pageIndex = 1;
        getShopGuider();
    }

    /**
     * 加载更多时
     */
    @Override
    public void onLoadMoreRequested() {
        pageIndex++;
        getShopGuider();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mShopGuiderAdapter !=null && mShopGuiderAdapter.isLoading()){
            mShopGuiderAdapter.loadMoreEnd();
        }
        refreshFinish();
    }
}
